home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / Miro_Downloader.exe / test / schematest.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2007-11-12  |  4.9 KB  |  142 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.5)
  3.  
  4. import datetime
  5. import os
  6. import tempfile
  7. import time
  8. import unittest
  9. import schema
  10. from schema import SchemaString, SchemaInt, SchemaFloat, SchemaBool
  11. from schema import SchemaDateTime, SchemaList, SchemaDict, SchemaObject
  12. from schema import SchemaSimpleContainer, ValidationError
  13. from test.framework import DemocracyTestCase
  14.  
  15. class TestValidation(DemocracyTestCase):
  16.     
  17.     def testModuleVariablesDefined(self):
  18.         self.assert_(hasattr(schema, 'VERSION'))
  19.         self.assert_(hasattr(schema, 'objectSchemas'))
  20.  
  21.     
  22.     def testNoneValidation(self):
  23.         self.assertRaises(ValidationError, SchemaInt(noneOk = False).validate, None)
  24.         self.assertRaises(ValidationError, SchemaInt().validate, None)
  25.         SchemaInt(noneOk = True).validate(None)
  26.  
  27.     
  28.     def testBoolValiation(self):
  29.         schemabool = SchemaBool()
  30.         self.assertRaises(ValidationError, schemabool.validate, 1)
  31.         self.assertRaises(ValidationError, schemabool.validate, 0)
  32.         self.assertRaises(ValidationError, schemabool.validate, 'True')
  33.         self.assertRaises(ValidationError, schemabool.validate, None)
  34.         schemabool.validate(True)
  35.         schemabool.validate(False)
  36.  
  37.     
  38.     def testDateTimeValiation(self):
  39.         schemadatetime = SchemaDateTime()
  40.         self.assertRaises(ValidationError, schemadatetime.validate, 1)
  41.         self.assertRaises(ValidationError, schemadatetime.validate, 0)
  42.         delta = datetime.timedelta(days = 40)
  43.         self.assertRaises(ValidationError, schemadatetime.validate, delta)
  44.         schemadatetime.validate(datetime.datetime(1980, 8, 1))
  45.  
  46.     
  47.     def testIntValiation(self):
  48.         schemaint = SchemaInt()
  49.         self.assertRaises(ValidationError, schemaint.validate, 'One')
  50.         self.assertRaises(ValidationError, schemaint.validate, 1.4)
  51.         schemaint.validate(1)
  52.         schemaint.validate(0x1L)
  53.  
  54.     
  55.     def testFloatValiation(self):
  56.         schemafloat = SchemaFloat()
  57.         self.assertRaises(ValidationError, schemafloat.validate, 'One half')
  58.         self.assertRaises(ValidationError, schemafloat.validate, 1)
  59.         schemafloat.validate(1.4)
  60.  
  61.     
  62.     def testStringValidation(self):
  63.         schemastring = SchemaString()
  64.         self.assertRaises(ValidationError, schemastring.validate, 10123)
  65.         self.assertRaises(ValidationError, schemastring.validate, '10123')
  66.         schemastring.validate(u'10123')
  67.  
  68.     
  69.     def testSimpleContainerValidation(self):
  70.         schemasimple = SchemaSimpleContainer()
  71.         schemasimple.validate({
  72.             1: u'Ben',
  73.             u'pie': 3.1415 })
  74.         schemasimple.validate([
  75.             1,
  76.             1,
  77.             u'two',
  78.             u'three',
  79.             5])
  80.         schemasimple.validate({
  81.             u'y2k': datetime.datetime(2000, 1, 1),
  82.             'now': time.localtime() })
  83.         schemasimple.validate({
  84.             'fib': (1, 1, u'two', u'three', 5),
  85.             'square': (1, 4, u'nine', 16),
  86.             'fact': (1, 2, 6, u'twenty-four') })
  87.         l = []
  88.         d = { }
  89.         l.extend([
  90.             l,
  91.             d])
  92.         d['list'] = l
  93.         schemasimple.validate(l)
  94.         schemasimple.validate(d)
  95.         
  96.         class TestObject(object):
  97.             pass
  98.  
  99.         self.assertRaises(ValidationError, schemasimple.validate, TestObject())
  100.         self.assertRaises(ValidationError, schemasimple.validate, [
  101.             TestObject()])
  102.         self.assertRaises(ValidationError, schemasimple.validate, {
  103.             'object': TestObject() })
  104.  
  105.     
  106.     def testListValidation(self):
  107.         schemalist = SchemaList(SchemaInt())
  108.         self.assertRaises(ValidationError, schemalist.validate, 1234)
  109.         schemalist.validate([
  110.             1,
  111.             2,
  112.             3,
  113.             4])
  114.  
  115.     
  116.     def testDictValidation(self):
  117.         schemadict = SchemaDict(SchemaInt(), SchemaString())
  118.         self.assertRaises(ValidationError, schemadict.validate, 1234)
  119.         schemadict.validate({
  120.             12: 'Buckle my shoe' })
  121.  
  122.     
  123.     def testObjectValidation(self):
  124.         
  125.         class TestObject(object):
  126.             pass
  127.  
  128.         
  129.         class ChildObject(TestObject):
  130.             pass
  131.  
  132.         schemaobject = SchemaObject(TestObject)
  133.         self.assertRaises(ValidationError, schemaobject.validate, 1234)
  134.         schemaobject.validate(TestObject())
  135.         schemaobject.validate(ChildObject())
  136.         self.assertRaises(ValidationError, schemaobject.validate, TestObject)
  137.  
  138.  
  139. if __name__ == '__main__':
  140.     unittest.main()
  141.  
  142.